{ "cells": [ { "metadata": {}, "cell_type": "markdown", "source": [ "# Iteterables II - Code Cards\n", "## Try me\n", "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ffraile/computer_science_tutorials/blob/main/source/Introduction/exercises/iterables%20II%20code%20cards.ipynb)[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/ffraile/computer_science_tutorials/main?labpath=source%2FIntroduction%2Fexercises%2Fiterables%20II%20code%20cards.ipynb)\n", "\n", "## How to use\n", "- Each card mirrors an A4 classroom prompt. **Predict first** (or discuss), then run the cell to check.\n", "- Detective cards show a buggy idea in Markdown; the code cell shows a **fixed** version.\n", "- Keep explanations short and schematic (*what* → *why*).\n", "\n", "### Turn Gemini into a coding tutor (no direct answers)\n", "Paste this in your first chat with Gemini to keep it in “tutor mode”:\n", "\n", "```markdown\n", "You are a **coding tutor** for Python in Jupyter/Colab. Follow the **course motto** “do not give up learning.”\n", "\n", "### Role & Goals\n", "- Use **Socratic guidance** and **test-first thinking** to help me solve problems myself.\n", "- Help me read errors, reason about state, and make small, safe iterations.\n", "\n", "### Strict Rules\n", "1) **Do not** provide full working solutions or paste complete functions/programs.\n", " - You may show **tiny illustrative fragments (≤3 lines)** or **pseudo-code with TODOs**, but not a drop-in answer.\n", "2) Prefer **questions over answers**; offer **one small next step** at a time.\n", "3) When debugging, explain **what the traceback says**, give **2–3 hypotheses**, and propose the **smallest diff** in *plain English* first.\n", "4) Encourage **TDD**: ask me to write/assert a test, predict, run, and report outputs.\n", "5) Keep responses concise (≈120–150 words) unless I ask for a deeper explanation or code review.\n", "6) Ask me to **run code and share results**; adapt based on the output.\n", "7) If I request the full solution, remind me of the rules and offer a **higher-tier hint** instead.\n", "8) When I finalize an exercise, reinforce learning lessons and suggest additional exercises\n", "\n", "### Interaction Loop (use this structure)\n", "- **Restate goal:** what I’m trying to accomplish in one line.\n", "- **Diagnose:** key assumption to check or error to interpret.\n", "- **Hint (tiered):**\n", " - Tier 1: Conceptual nudge (no code).\n", " - Tier 2: Directed hint (identify line/construct to change).\n", " - Tier 3: Pseudo-code with TODOs or a **1–3 line** pattern (still not a full solution).\n", "- **Next action:** one concrete step for me to try now.\n", "- **Ask back:** what to run/paste (output, test result, or traceback).\n", "\n", "### When reviewing my code\n", "- Comment on **correctness, clarity, naming, and complexity (big-O)**.\n", "- Suggest **tests** I’m missing (boundaries, empty cases, error paths).\n", "\n", "### Safety & Ethics\n", "- No secrets or private data in prompts.\n", "- avoid library functions/APIs unless I ask.\n", "\n", "Stay in tutor mode for the whole session.\n", "```\n", "\n", "## Code Cards\n", "1. Does any statement raise an exception? (Explain why)\n", "```python\n", "t = (\"A\", \"B\", \"C\")\n", "y = [\"D\", \"E\", \"F\"]\n", "y = y + [\"G\"]\n", "t[0] = \"Z\"\n", "```" ], "id": "d9e1354908cad180" }, { "metadata": {}, "cell_type": "code", "source": "", "id": "c9e5e981bf9bbfab", "outputs": [], "execution_count": null }, { "metadata": {}, "cell_type": "markdown", "source": [ "2. Does any statement raise an exception? (Explain why)\n", "```python\n", "t = (\"A\", \"B\", \"C\")\n", "a = t[0]\n", "a += \"DD\"\n", "t[0] = \"Z\"\n", "```" ], "id": "3608a5e446587830" }, { "metadata": {}, "cell_type": "code", "outputs": [], "execution_count": null, "source": "", "id": "4494afe78d65cdb4" }, { "metadata": {}, "cell_type": "markdown", "source": [ "\n", "3. Given:\n", "\n", "```python\n", "real_names = {\"Homelander\": \"John\", \"Queen Maeve\": \"Maggie\", \"A-Train\": \"Reggie\", \"Starlight\": \"Annie\"}\n", "```\n", "Choose all expressions below that evaluate to True:\n", "```python\n", "\"A-Train\" in real_names\n", "\"John\" in real_names\n", "\"Maggie\" in real_names.values()\n", "\"Annie\" in real_names.items()\n", "```" ], "id": "be30cefb031715b9" }, { "metadata": {}, "cell_type": "code", "outputs": [], "execution_count": null, "source": "", "id": "19b153aad0773b89" }, { "metadata": {}, "cell_type": "markdown", "source": [ "4. Given:\n", "```python\n", "supes = {\"Homelander\": {\"real_name\": \"John\", \"power_level\": 100},\n", " \"Queen Maeve\": {\"real_name\": \"Maggie\", \"power_level\": 85},\n", " \"A-Train\": {\"real_name\": \"Reggie\", \"power_level\": 75},\n", " \"Starlight\": {\"real_name\": \"Annie\", \"power_level\": 80}}\n", "```\n", "Choose all expressions below that evaluate to True:\n", "```python\n", "\"power_level\" in supes[\"A-Train\"]\n", "\"Reggie\" in supes[\"A-Train\"].values()\n", "\"Annie\" in supes[\"Starlight\"].keys()\n", "supes[\"Homelander\"][\"power_level\"] < supes[\"Queen Maeve\"][\"power_level\"]\n", "```\n" ], "id": "2d17833c50fff38c" }, { "metadata": {}, "cell_type": "code", "outputs": [], "execution_count": null, "source": "", "id": "c147764e1b8d1f20" }, { "metadata": {}, "cell_type": "markdown", "source": [ "5. Given:\n", "```python\n", "students = {\"001\": {\"name\": \"Peter Parker\", \"grades\": [85, 90, 78]},\n", " \"002\": {\"name\": \"Jean Grey\", \"grades\": [88, 76, 92]},\n", " \"003\": {\"name\": \"Alonzo Thompson\", \"grades\": [95, 85, 87]}}\n", "```\n", "Select the best way to iterate through the students dictionary to calculate and print each student's average grade together with their id and name. Motivate your response.\n", "```python\n", "# Option A\n", "for student_id in students:\n", " info = students[student_id]\n", " for grade in info[\"grades\"]:\n", " avg_grade = sum(info[\"grades\"]) / len(info[\"grades\"])\n", " print(f\"ID: {student_id}, Name: {info['name']}, Average Grade: {avg_grade:.2f}\")\n", "# Option B\n", "for student_id, info in students.items():\n", " avg_grade = sum(info[\"grades\"]) / len(info[\"grades\"])\n", " print(f\"ID: {student_id}, Name: {info['name']}, Average Grade: {avg_grade:.2f}\")\n", "\n", "# Option C\n", "for info in students.values():\n", " avg_grade = sum(info[\"grades\"]) / len(info[\"grades\"])\n", " print(f\"Name: {info['name']}, Average Grade: {avg_grade:.2f}\")\n", "```\n", "\n" ], "id": "5aef896e67b6cb87" }, { "metadata": {}, "cell_type": "code", "outputs": [], "execution_count": null, "source": "", "id": "7e981139fb2c90f4" }, { "metadata": {}, "cell_type": "markdown", "source": [ "6. Given:\n", "```python\n", "students = {\"001\": {\"name\": \"Peter Parker\", \"grades\": [85, 90, 78]},\n", " \"002\": {\"name\": \"Jean Grey\", \"grades\": [88, 76, 92]},\n", " \"003\": {\"name\": \"Alonzo Thompson\", \"grades\": [95, 85, 87]}}\n", "```\n", "\n", "Predict the output of the following code snippet:\n", "```python\n", "print(students[\"002\"][\"grades\"][1])\n", "print(len(students))\n", "print(\"Jean Grey\" in students[\"002\"])\n", "```\n" ], "id": "65c6895dc0e37429" }, { "metadata": {}, "cell_type": "code", "outputs": [], "execution_count": null, "source": "", "id": "6f98ce8f2a3d008c" }, { "metadata": {}, "cell_type": "markdown", "source": [ "7. Given:\n", "```python\n", "supes = {\"Homelander\": {\"real_name\": \"John\", \"power_level\": 100},\n", " \"Queen Maeve\": {\"real_name\": \"Maggie\", \"power_level\": 85},\n", " \"A-Train\": {\"real_name\": \"Reggie\", \"power_level\": 75},\n", " \"Starlight\": {\"real_name\": \"Annie\", \"power_level\": 80}}\n", "```\n", "Predict the output of the following code snippet:\n", "```python\n", "print(supes[\"Startlight\"][\"power_level\"])\n", "print(supes[\"A-Train\"][\"real_name\"])\n", "for supe in supes:\n", " if supes[supe][\"power_level\"] > 80:\n", " print(f\"{supe} is powerful\")\n", "```\n" ], "id": "e3f579b21561fd63" }, { "metadata": {}, "cell_type": "code", "outputs": [], "execution_count": null, "source": "", "id": "e899dac818c5bf7c" }, { "metadata": {}, "cell_type": "markdown", "source": [ "8. Given:\n", "```python\n", "pilots = [{\"name\": \"Han Solo\", \"ship\": \"Millennium Falcon\", \"speed\": 1050, \"badges\": [\"Smuggler\", \"Gunner\"]},\n", " {\"name\": \"Luke Skywalker\", \"ship\": \"X-Wing\", \"speed\": 950, \"badges\": [\"Jedi\", \"Pilot\"]},\n", " {\"name\": \"Darth Vader\", \"ship\": \"TIE Fighter\", \"speed\": 900, \"badges\": [\"Sith Lord\", \"Commander\"]}]\n", "```\n", "Select the best way to iterate through the pilots list to print each pilot's name, ship, and badges. Motivate your response.\n", "```python\n", "# Option A\n", "for pilot in pilots:\n", " print(f\"Name: {pilot['name']}, Ship: {pilot['ship']}, Badges: {', '.join(pilot['badges'])}\")\n", "# Option B\n", "for i in range(len(pilots)):\n", " pilot = pilots[i]\n", " print(f\"Name: {pilot['name']}, Ship: {pilot['ship']}, Badges: {', '.join(pilot['badges'])}\")\n", "# Option C\n", "for pilot in pilots:\n", " for k, v in pilot.items():\n", " print(f\"{k}: {v}\")\n", "```\n" ], "id": "fce8ecebc4fd2525" }, { "metadata": {}, "cell_type": "code", "outputs": [], "execution_count": null, "source": "", "id": "519b6dccef4f8d25" }, { "metadata": {}, "cell_type": "markdown", "source": [ "9. Predict the output of the following code snippet:\n", "```python\n", "pilots = [{\"name\": \"Han Solo\", \"ship\": \"Millennium Falcon\", \"speed\": 1050, \"badges\": [\"Smuggler\", \"Gunner\"]},\n", " {\"name\": \"Luke Skywalker\", \"ship\": \"X-Wing\", \"speed\": 950, \"badges\": [\"Jedi\", \"Pilot\"]},\n", " {\"name\": \"Darth Vader\", \"ship\": \"TIE Fighter\", \"speed\": 900, \"badges\": [\"Sith Lord\", \"Commander\"]}]\n", "print(pilots[1][\"badges\"][0])\n", "print(pilots[2][\"speed\"] > pilots[0][\"speed\"])\n", "print(len(pilots[0][\"badges\"][1]))\n", "```\n", "\n" ], "id": "8a4c4c8f7562aea6" }, { "metadata": {}, "cell_type": "code", "source": "", "id": "76385679641faeb1", "outputs": [], "execution_count": null }, { "metadata": {}, "cell_type": "markdown", "source": [ "10. Which of the following snippets produces the same result as the others?\n", "```python\n", "# Option A\n", "series = []\n", "for i in range(10):\n", " if i > 0 and i % 3 == 0:\n", " series.append(1/i**3)\n", "print(series)\n", "\n", "# Option B\n", "series = [i%3 == 0 for i in range(10) if i > 0 and 1/i**3 == 0]\n", "print(series)\n", "\n", "# Option C\n", "series = [1/i**3 for i in range(10) if i > 0 and i % 3 == 0]\n", "print(series)\n", "\n", "# Option D\n", "series = [1/i**3 if i > 0 and i % 3 == 0 else 0 for i in range(10)]\n", "print(series)\n", "```" ], "id": "69b37dcef9fc38ce" }, { "metadata": {}, "cell_type": "code", "source": "", "id": "b333f95ce012babf", "outputs": [], "execution_count": null }, { "metadata": {}, "cell_type": "markdown", "source": [ "11. What is the series of numbers generated by the following code snippet? Briefly explain how it works.\n", "```python\n", "s = [i**2 for i in range(5) if i % 2 == 0]\n", "```\n", "**A** The series is: ```[0, 1, 4, 9, 16]```\n", "\n", "**B** The series is: ```[1, 4, 9, 16, 25]```\n", "\n", "**C** The series is: ```[0, 4, 16]```\n", "\n", "**D** The series is Fall Out season 3 premiering November 2026." ], "id": "3388b99c3e0230f4" }, { "metadata": {}, "cell_type": "code", "outputs": [], "execution_count": null, "source": "", "id": "1c335920478c42ec" }, { "metadata": {}, "cell_type": "markdown", "source": [ "12. What is the series of numbers generated by the following code snippet? Briefly explain how it works.\n", "```python\n", "fib = [0, 1] + [fib.append(sum(fib[-2:])) for _ in range(5)]\n", "```\n", "**A** The series is: ```[0, 1, 2, 3, 4, 5, 6]```\n", "\n", "**B** The series is: ```[0, 1, 1, 2, 3, 5, 8]```\n", "\n", "**C** The series is: ```[0, 1, 1, 2, 4, 8, 16```\n", "\n", "**D** The code raises an exception." ], "id": "cc19f41e143cde83" }, { "metadata": {}, "cell_type": "code", "outputs": [], "execution_count": null, "source": "", "id": "43310a432ab96f3d" }, { "metadata": {}, "cell_type": "markdown", "source": [ " 13. What is the series of numbers generated by the following code snippet? Briefly explain how it works.\n", "```python\n", "series = [2**i for i in range(6) if i % 2 == 1]\n", "```\n", "**A** The series is: ```[1, 2, 4, 8, 16, 32]```\n", "\n", "**B** The series is: ```[2, 8, 32]```\n", "\n", "**C** The series is: ```[2, 4, 6, 8, 10]```\n", "\n", "**D** The code raises an exception." ], "id": "80e85953d87146b1" }, { "metadata": {}, "cell_type": "code", "outputs": [], "execution_count": null, "source": "", "id": "8f4740648320b2af" } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 5 }